/**
 * Created by 黄忠园 on 2017/12/25.
 */
angular.module('service.job.jobInfo', [
        'ngResource',
        'ngCookies'
    ])
    .service('JobJobInfoService', JobJobInfoService);
JobJobInfoService.$inject = ['$resource'];
function JobJobInfoService($resource) {

    // 新增工单
    this.addJob = function (info, success, error) {
        var resource = $resource(joburl + "/jobInfo");
        return resource.save({}, info, success, error);
    };

    // 根据shopId查询
    this.getAllOrder = function (shopid,pageNo, success, error) {
        var resource = $resource(joburl + "/jobInfo/findByShopId/:shopId/:pageNo", {}, {
            query: {
                method: 'GET',
                isArray: true,
                transformResponse: function (data, headers) {
                    var list = JSON.parse(data);
                    for (var index in list) {
                        list[index].commonality = JSON.parse(list[index].commonality);
                        list[index].repairman = {
                            name: "",
                            phone: "",
                            time: ""
                        }
                    }
                    return list;
                }
            }
        });
        return resource.query({shopId: shopid,pageNo:pageNo}, success, error);
    };

    // 根据shopId，jobstatus查询
    this.getAllOrderByJobstatus = function (shopid,jobstatus,pageNo, success, error) {
        var resource = $resource(joburl + "/jobInfo/findByShopIdAndJobStatus/:shopId/:jobstatus/:pageNo", {}, {
            query: {
                method: 'GET',
                isArray: true,
                transformResponse: function (data, headers) {
                    var list = JSON.parse(data);
                    for (var index in list) {
                        list[index].commonality = JSON.parse(list[index].commonality);
                        list[index].repairman = {
                            name: "",
                            phone: "",
                            time: ""
                        }
                    }
                    return list;
                }
            }
        });
        return resource.query({shopId: shopid,pageNo:pageNo,jobstatus:jobstatus}, success, error);
    };

    // 根据jobid查询工单
    this.getOrderByJobId = function (jobId, success, error) {
        var resource = $resource(joburl + "/jobInfo/:jobId", {}, {
            get: {
                method: 'GET',
                isArray: false,
                transformResponse: function (data, headers) {
                    data = JSON.parse(data);
                    data.commonality = JSON.parse(data.commonality);
                    data.repairman = {
                        name: "",
                        phone: "",
                        time: ""
                    };
                    return data;
                }
            }
        });
        return resource.get({jobId: jobId}, success, error);
    };

    // 根据工单Id,修改工单负责人（必须传入jobId、platformId、teamId、dutyId）
    this.modifyOrder1 = function (info, success, error) {
        var resource = $resource(joburl + "/jobInfo/updateDutyByJobId");
        return resource.save({}, info, success, error);
    };

    // 根据工单Id,修改工单状态（必须传入jobId、jobStatus）
    this.modifyOrder2 = function (info, success, error) {
        var resource = $resource(joburl + "/jobInfo/updateJobStatusByJobId");
        return resource.save({}, info, success, error);
    }
}